/*
 *所有关于yjy_whole_info类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*yjy_whole_infoService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Finance.IRepositories;
using VOL.Core.DBManager;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System;
using System.Collections.Generic;

namespace VOL.Finance.Services
{
    public partial class yjy_whole_infoService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Iyjy_whole_infoRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public yjy_whole_infoService(
            Iyjy_whole_infoRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            WebResponseContent webResponse = new WebResponseContent();
            if (keys == null)
            {
                return webResponse.Error("参数不能为空,请重新操作！");
            }
            var dapper = DBServerProvider.SqlDapper;
            string yjy_whole_info_ids = "";
            string materialnumstr = "";
            foreach (var item in keys)
            {
                yjy_whole_info_ids = yjy_whole_info_ids+"'" + item + "',";
                List<yjy_whole_info> infoList2 = DBServerProvider.DbContext.Set<yjy_whole_info>().Where(x => x.price_state == 0).ToList();
                yjy_whole_info info = DBServerProvider.DbContext.Set<yjy_whole_info>().Where(x => x.yjy_whole_info_id == Guid.Parse(item.ToString())).FirstOrDefault();
                materialnumstr = materialnumstr + "'" + info.materialnum + "',";
            }
            yjy_whole_info_ids = yjy_whole_info_ids.Substring(0, yjy_whole_info_ids.Length-1);
            materialnumstr = materialnumstr.Substring(0, materialnumstr.Length - 1);
            try
            {
                dapper.BeginTrans();
                string sql = "delete from yjy_whole_data_analysis where yjy_whole_info_id in (" + yjy_whole_info_ids + ")";
                var data = dapper.ExcuteNonQuery(sql, null);
                string sql1 = "delete from yjy_whole_cost where yjy_whole_info_id in ("+ yjy_whole_info_ids + ")";
                var data1 = dapper.ExcuteNonQuery(sql1, null);
                string sql2 = "delete from yjy_whole_info where yjy_whole_info_id in ("+ yjy_whole_info_ids + ")";
                var data2 = dapper.ExcuteNonQuery(sql2, null);
                string sql3 = "delete from yjy_whole_wlzb where materialnum in ("+materialnumstr+")";
                var data3 = dapper.ExcuteNonQuery(sql3,null);
                dapper.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                dapper.Rollback();
                return webResponse.Error("删除失败，请重新再试！");
            }
            return webResponse.OK("删除成功！");
        }
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            WebResponseContent webResponse = new WebResponseContent();  
            string materialnum = (string)saveDataModel.MainData["materialnum"];
            if (string.IsNullOrEmpty(materialnum))
            {
                return webResponse.Error("参数不能为空，请重新输入！");
            }
            string sql = "SELECT a.materialnum FROM yjy_whole_cost a WHERE a.materialnum = @materialnum UNION ALL SELECT b.materialnum FROM yjy_whole_info b WHERE b.materialnum = @materialnum";
            List<dynamic> list = DBServerProvider.SqlDapper.QueryList<dynamic>(sql,new { materialnum = materialnum}).ToList();
            if(list!=null && list.Count > 0)
            {
                return webResponse.Error("整车头物料号已经存在，不能再进行添加！");
            }
            AddOnExecuting = (yjy_whole_info order, object list) =>
            {
                order.status = 0;
                order.is_delele = 0;
                order.bom_state = 0;
                order.price_state = 0;
                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }
    }
}
